package com.mjk.datasource;

import com.mjk.api.dto.ApiDto;
import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.request.BaseDto;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.base.work.BaseWork;
import com.mjk.common.base.work.annotation.BeeApi;
import com.mjk.common.base.work.annotation.BeeWork;
import com.mjk.common.core.jdbc.service.plus.idbservice.BaseDBService;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.util.TimeUtils;
import com.mjk.datasource.model.MiProperties;
import com.mjk.datasource.model.MiTempProperties;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 单表操作 测试用例
 */
@BeeWork(work = "simpletable")
public class SimpleTableExample extends BaseWork {
    @Autowired
    private BeeDBService beeDBService;

    @Autowired
    private BaseDBService baseDBService;

    /**
     * 查询用法 可以选着BeeDBService 和 BaseDBService中的任意一个
     */
    @BeeApi(setOnNull = false)
    public BeeResponse getSingleTableInfo(ApiDto dto){
        BeeQueryWrapper wrapper = new BeeQueryWrapper(new MiProperties());
//        wrapper.select(MiTempProperties::getFIXMEDINS_CODE1);
//        wrapper.setPage(1, 10);
        //wrapper.eq(MiProperties::getFIXMEDINS_CODE,"H33010900689");
        //wrapper.condition("MI_DOMAIN=#{aaa} or MI_DOMAIN=#{bbb}","111","222");
        List<MiProperties> properties = beeDBService.findManyByWrapper(wrapper);
        // System.out.println(dto.getTime().format(DateTimeFormatter.ofPattern(TimeUtils.DATE_FORMAT_DATEONLY)));
        return BeeError.BEE_SUC.tranceError(properties);
    }

    /**
     * 更新 可以选着BeeDBService 和 BaseDBService中的任意一个
     */
    public BeeResponse updateSingleTable(BaseDto dto){
        MiProperties tmp = new MiProperties();
        tmp.setFIXMEDINS_CODE("111");
        BeeQueryWrapper wrapper = new BeeQueryWrapper(tmp);
        wrapper.inc(MiProperties::getFIXMEDINS_CODE);
        wrapper.eq(MiProperties::getFIXMEDINS_NAME,"萧山区皮肤病医院");
        beeDBService.update(wrapper);
        return BeeError.BEE_SUC.tranceError();
    }

    /**
     * 批量更新 不要使用baseDBService 使用beeDBService
     */
    public BeeResponse batchUpdateSingleTable(BaseDto dto){
        MiProperties tmp = new MiProperties();
        tmp.setBAK1("11");
        tmp.setFIXMEDINS_CODE("H33010900249");
        tmp.setFIXMEDINS_NAME("萧山区皮肤病医院1");
        MiProperties tmp1 = new MiProperties();
        tmp1.setBAK1("11");
        tmp1.setFIXMEDINS_CODE("111");
        tmp1.setFIXMEDINS_NAME("萧山区皮肤病医院2");
        List<MiProperties> properties = new ArrayList<>();
        properties.add(tmp);
        properties.add(tmp1);
        beeDBService.update(properties);
        return BeeError.BEE_SUC.tranceError();
    }
    /**
     * 插入用法 可以选着BeeDBService 和 BaseDBService中的任意一个
     */
    public BeeResponse insertSingleTable(BaseDto dto){
        MiProperties tmp = new MiProperties();
        tmp.setFIXMEDINS_NAME("test");
//        baseDBService.save(tmp);
        beeDBService.save(tmp);
        return BeeError.BEE_SUC.tranceError();
    }

    /**
     * 批量插入
     */
    public BeeResponse batchInsertSingleTable(BaseDto dto){
        List<MiProperties> properties = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            MiProperties tmp = new MiProperties();
            tmp.setFIXMEDINS_NAME("test"+i);
            if (i % 2 == 0) {
                tmp.setBAK2("1111");
            }
            properties.add(tmp);
        }
        long start = System.currentTimeMillis();
        beeDBService.save(properties);
        System.out.println("数据插入耗时:"+(System.currentTimeMillis()-start));
        return BeeError.BEE_SUC.tranceError();
    }
    /**
     * 删除用法
     */
    public BeeResponse delSingleTable(BaseDto dto){
        BeeQueryWrapper wrapper = new BeeQueryWrapper(new MiProperties());
        wrapper.eq(MiProperties::getFIXMEDINS_NAME,"test111");
        beeDBService.delete(wrapper);


        return BeeError.BEE_SUC.tranceError();
    }

    /**
     * 批量删除
     */
    public BeeResponse batchDelSingleTable(BaseDto dto){
        List<MiProperties> properties = new ArrayList<>();
        MiProperties tmp = new MiProperties();
        tmp.setFIXMEDINS_NAME("test111");
        //tmp.setFIXMEDINS_CODE("111");
        MiProperties tmp1 = new MiProperties();
        tmp1.setFIXMEDINS_NAME("test");
      //  tmp1.setBAK1("111");
        properties.add(tmp);
        properties.add(tmp1);
        beeDBService.delete(properties);
        return BeeError.BEE_SUC.tranceError();
    }
}
